﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace Help
{
    public class PubConstant
    {
        /// <summary>
        /// 获取连接字符串
        /// </summary>
        public static string ConnectionString
        {
            get
            {
                string _connectionString = ConfigurationManager.AppSettings["main_conn_string"];

                //string _connectionString = ConnectionFileHelper.getMainConnection();
                string ConStringEncrypt = ConfigurationManager.AppSettings["ConStringEncrypt"];
                if (ConStringEncrypt == "true")
                {
                    _connectionString = DESEncrypt.Decrypt(_connectionString);
                }
                return _connectionString;
            }
        }

        /// <summary>
        /// 得到web.config里配置项的数据库连接字符串。
        /// </summary>
        /// <param name="configName"></param>
        /// <returns></returns>
        public static string GetConnectionString(string configName)
        {
            string connectionString = ConfigurationManager.AppSettings[configName];
            string ConStringEncrypt = ConfigurationManager.AppSettings["ConStringEncrypt"];
            if (ConStringEncrypt == "true")
            {
                connectionString = DESEncrypt.Decrypt(connectionString);
            }
            return connectionString;
        }

        public static bool IsCanOpenConnection()
        {
            bool flag = false;
            try
            {
                SqlConnection conn = new SqlConnection(ConnectionString);
                if (conn.State != ConnectionState.Open)
                {
                    try
                    {
                        conn.Open();
                        flag = true;
                    }
                    catch (Exception ex)
                    {
                        flag = false;
                        Console.Write(ex.StackTrace.ToString());
                    }



                }
            }
            catch (ArgumentException aex)
            {
                Console.Write(aex.StackTrace.ToString());
            }
            return flag;
        }


    }
}
